//
//  HomeViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/4.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "HomeViewController.h"
#import <SDCycleScrollView.h>
#import "HomeTypeOneCell.h"
#import <MJRefresh.h>
#import "CustomViewOfCircle.h"
#import "XZDrawHomeView.h"

@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
{
    NSInteger number;
}
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@end

@implementation HomeViewController
+ (instancetype)initWithImageHomeViewController:(NSMutableArray *)iamgeMArray arrtwork:(NSMutableArray *)artworkMArray {
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.iamgeMArray = iamgeMArray;
    homeVC.artworkMArray = artworkMArray;
    return homeVC;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[[UIImage imageNamed:@"iphone_tabbar_colors_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iphone_tabbar_colors_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.cycleScrollView adjustWhenControllerViewWillAppera];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"首页";
    
    self.imageArray = @[@"http://www.wohoney.com/images/case_img_74.png", @"http://www.wohoney.com/images/case_img_72.png", @"http://www.wohoney.com/images/case_img_71.png", @"http://www.wohoney.com/images/case_img_82.png"];
    
    NSArray *imageTwoArray = @[@"http://web.wohoney.com/status_686734963504054272_1543913712.jpg", @"http://web.wohoney.com/status_686734963504054272_1543913700.jpg", @"http://web.wohoney.com/status_686734963504054272_1543913681.jpg", @"http://web.wohoney.com/status_686734963504054272_1543913672.jpg"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeTypeOneCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HomeTypeOneCell"];
    [self setTableViewUI];
}

- (void)setTableViewUI {
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    SDCycleScrollView *cycleScrollView = [cycleScrollViewWithFrame:frame delegate:delegate placeholderImage:[UIImage ]];
//    cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    
    NSArray *idleImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"black1.tiff"], [UIImage imageNamed:@"black2.tiff"], [UIImage imageNamed:@"black3.tiff"], [UIImage imageNamed:@"black4.tiff"], [UIImage imageNamed:@"black5.tiff"], [UIImage imageNamed:@"black6.tiff"], nil];
    NSArray *pullingImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"black1.tiff"], [UIImage imageNamed:@"black2.tiff"], [UIImage imageNamed:@"black3.tiff"], [UIImage imageNamed:@"black4.tiff"], [UIImage imageNamed:@"black5.tiff"], [UIImage imageNamed:@"black6.tiff"], nil];
//    NSArray *refreshingImages = @[];
    NSArray *refreshingImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"black1.tiff"], [UIImage imageNamed:@"black2.tiff"], [UIImage imageNamed:@"black3.tiff"], [UIImage imageNamed:@"black4.tiff"], [UIImage imageNamed:@"black5.tiff"], [UIImage imageNamed:@"black6.tiff"], nil];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 设置普通状态的动画图片
//    [header setImages:idleImages forState:MJRefreshStateIdle];
//    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
//    [header setImages:pullingImages forState:MJRefreshStatePulling];
//    // 设置正在刷新状态的动画图片
//    [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
    
    // 设置文字
//    [header setTitle:@"下拉可以刷新" forState:MJRefreshStateIdle];
//    [header setTitle:@"松开立即刷新" forState:MJRefreshStatePulling];
//    [header setTitle:@"正在刷新数据中......" forState:MJRefreshStateRefreshing];
//    header.lastUpdatedTimeLabel.hidden = YES;
//    
//    // 设置字体
//    header.stateLabel.font = [UIFont systemFontOfSize:15];
//    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置颜色
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width / 728 * 426) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    self.cycleScrollView.imageURLStringsGroup = self.imageArray;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    self.cycleScrollView.autoScrollTimeInterval = 4.0;
    self.cycleScrollView.placeholderImage = [UIImage imageNamed:@""];
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.tableView.tableHeaderView = self.cycleScrollView;
}

- (void)loadNewData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)loadMoreData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
    });
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    [self.navigationController pushViewController:[NSClassFromString(@"DemoVCWithXib") new] animated:YES];
}

// 滚动到第几张图回调
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    NSLog(@">>>>>> 滚动到第%ld张图", (long)index);
}

#pragma mark --tableViewDelegate--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld分区, 第%ld行", indexPath.section, indexPath.row];
        return cell;
    }else if (indexPath.section == 0) {
        HomeTypeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTypeOneCell"];
        if (!cell) {
            cell = (HomeTypeOneCell *)[[NSBundle mainBundle] loadNibNamed:@"HomeTypeOneCell" owner:nil options:nil];
        }
        return cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"第%ld分区, 第%ld行", indexPath.section, indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return Screen_Width / 2 + 20 + 20 + 20 + 40 + 10;
    }else if (indexPath.section == 1) {
        return 60;
    }
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSLog(@"点击了第%ld分区, 第%ld个Cell", indexPath.section, indexPath.row);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
